import GlobeTooltip from './GlobeTooltip'
import  * as Cesium from 'cesium';
var drawPointHelp = function () {
    this.init.apply(this, arguments);
};
drawPointHelp.prototype = {
    viewer: null,
    drawHandler: null,
    init:function(viewer){
        var _this = this;
        _this.viewer = viewer;
        _this.scene = viewer.scene;
        _this.clock = viewer.clock;
        _this.canvas = viewer.scene.canvas;
        _this.camera = viewer.scene.camera;
        _this.ellipsoid = viewer.scene.globe.ellipsoid;
        _this.tooltip = new GlobeTooltip(viewer.container);
    },
    active:function(callback){
        var _this = this;
        _this.drawHandler = new Cesium.ScreenSpaceEventHandler(_this.canvas);
        _this.drawHandler.setInputAction(function (event) {
            var wp = event.position;
            if (!Cesium.defined(wp)) {
                return;
            }
            var cartesian = _this.scene.pickPosition(wp)
            if (!Cesium.defined(cartesian)) {
                return;
            }
            _this.viewer.scene.clampToHeightMostDetailed([cartesian]).then(function (clampedCartesians) {
                var cartesian3Value = clampedCartesians[0]
                var cartographicValue = Cesium.Cartographic.fromCartesian(cartesian3Value)
                var latlng = [Cesium.Math.toDegrees(cartographicValue.latitude),Cesium.Math.toDegrees(cartographicValue.longitude),cartographicValue.height]
                callback({
                    cartesian3:cartesian3Value,
                    cartographic:cartographicValue,
                    latlng:latlng
                })
                _this.drawEnd()
            })
        }, Cesium.ScreenSpaceEventType.LEFT_CLICK);
        _this.drawHandler.setInputAction(function (event) {
            var wp = event.endPosition;
            if (!Cesium.defined(wp)) {
                return;
            }
            _this.tooltip.showAt(wp, "<p>选择位置</p>");  
        }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
    },
    disactive:function(){

    },
    drawEnd:function(){
        this.tooltip.setVisible(false);
        if (this.drawHandler) {
            this.drawHandler.destroy();
            this.drawHandler = null;
        }
    }
}
export default drawPointHelp